var shallMenu = function () {
    this.html = $('<div id="shallMenu"><h1>shallMenu</h1></div>');
    this.items = {
        rename: {
            name: '重命名',
            event: function () { }
        },
        delete: {
            name: '删除',
            event: function () { }
        },
        newFolder: {
            name: '新建文件夹',
            event: function () { }
        },
        move: {
            name: '移动',
            event: function () { }
        },
        other: {
            name: '复制为新接口',
            event: function () { }
        },
        closeOther: {
            name: '关闭其他标签',
            event: function () { }
        },
    };
    this.folderArray = ['newFolder', 'move', 'rename', 'delete'];
    this.notFolderArray = ['move', 'delete', 'other'];
    this.tabArray = ['closeOther'];
    this.html = null;
};
shallMenu.prototype = {
    constructor: this,
    render: function () {
        return this.html;
    },
    on: function (str, event) {
        if (this.items.hasOwnProperty(str)) {
            this.items[str].event = event;
        };
    },
    render: function (event, isFolder, obj) {
        this.hide();
        this.html = $('<div class="contextmenu"></div>');
        global.document.onclick = () => {
            this.hide();
        };
        if (isFolder === 'tab') {
            $.each(['closeOther'], (index, item) => {
                this.html.append(this.renderItem(item, obj));
            })
        } else if (isFolder === null) {
            $.each(['newFolder'], (index, item) => {
                this.html.append(this.renderItem(item, obj));
            })
        } else if (isFolder) {
            $.each(this.folderArray, (index, item) => {
                this.html.append(this.renderItem(item, obj));
            })
        } else {
            $.each(this.notFolderArray, (index, item) => {
                this.html.append(this.renderItem(item, obj));
            });
        };
        var height = 4 * 35 + 2;
        var winHeight = $(window).height();
        if ((event.clientY + height) > winHeight) {
            this.html.css({
                left: event.clientX,
                bottom: winHeight - event.clientY
            });
        } else {
            this.html.css({
                left: event.clientX,
                top: event.clientY
            });
        };
        $('body').append(this.html);
    },
    renderItem: function (type, obj) {
        var itemData = this.items[type];
        var itemHtml = $('<div class="item">' + itemData.name + '</div>').click((e) => {
            itemData.event(obj);
        });
        return itemHtml;
    },
    hide: function () {
        if (this.html) {
            this.html.remove();
            this.html = null;
            global.document.onclick = function () { }
        };
    }
};
module.exports = function () {
    return new shallMenu();
};